<template>
  <el-dialog title="设置角色" :visible="isCheckbox" @close="closeFn">
    <el-checkbox-group v-model="checkRoles">
      <el-checkbox
        v-for="item in rolesList"
        :key="item.id"
        :label="item.id"
      >{{ item.title }}</el-checkbox>
    </el-checkbox-group>
    <span slot="footer">
      <el-row type="flex" justify="end">
        <el-button type="primary" @click="okBtn">确定</el-button>
        <el-button @click="closeFn">取消</el-button>
      </el-row>
    </span>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/roles'
import { getEmployeeById, updateEmployee } from '@/api/employees'
export default {
  props: {
    isCheckbox: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      rolesList: [],
      checkRoles: [],
      infoId: {}
    }
  },
  async created() {
    const res = await getRoleList()
    console.log(31, res.data)
    this.rolesList = res.data
  },
  methods: {
    // 根据id获取用户角色
    async getRoles(id) {
      this.checkRoles = []
      const res = await getEmployeeById(id)
      console.log(45, res.data)
      if (res.data.roles.length > 0) {
        res.data.roles.forEach(item => {
          this.checkRoles.push(item.id)
        })
      }
      this.infoId = res.data
    },
    // 关闭弹窗
    closeFn() {
      this.$emit('update:isCheckbox', false)
    },
    // 确认
    async okBtn() {
      this.infoId.roles = this.checkRoles
      const res = await updateEmployee(this.infoId)
      console.log(59, res)
      this.closeFn()
    }
  }
}
</script>

<style>

</style>
